package jgl.vajra.kingkong.part.vehicle.activity;

import android.os.Parcelable;
import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.ChangeDriverReasonBean;
import jgl.vajra.kingkong.bean.MarketPriceBean;
import jgl.vajra.kingkong.bean.OrderParametersBean;
import jgl.vajra.kingkong.constant.Constant;
import jgl.vajra.kingkong.databinding.ActivityChangeDriverBinding;
import jgl.vajra.kingkong.databinding.ItemChangeDriverBinding;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.home.activity.HomeActivity;
import jgl.vajra.kingkong.part.vehicle.contract.ChangeDriverContract;
import jgl.vajra.kingkong.part.vehicle.viewModel.ChangeDriverViewModel;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.BaseDataBindingDecorator;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.AppActivityManager;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.utils.PhoneUtils;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(ChangeDriverViewModel.class)
public class ChangeDriverActivity extends BaseMVVMActivity<ChangeDriverViewModel, ActivityChangeDriverBinding> implements ChangeDriverContract.View, BaseBindingItemPresenter<ChangeDriverReasonBean.ReasonTextBean> {

    private SingleTypeBindingAdapter adapter;
    private List<ChangeDriverReasonBean> list;
    private String reasonText;
    private String orderId;
    private OrderParametersBean orderParameters;
    private MarketPriceBean marketPriceBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_driver;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        orderId = getIntent().getStringExtra("orderId");
        orderParameters = getIntent().getParcelableExtra("orderParameters");
        marketPriceBean = getIntent().getParcelableExtra("marketPriceBean");

        list = new ArrayList<>();
        ChangeDriverReasonBean bean = new ChangeDriverReasonBean();
        bean.title = "货物或机械原因";
        List<ChangeDriverReasonBean.ReasonTextBean> reasonBeanList = new ArrayList<>();
        reasonBeanList.add(new ChangeDriverReasonBean.ReasonTextBean("机械装不下货物", false));
        reasonBeanList.add(new ChangeDriverReasonBean.ReasonTextBean("不是我想要的车型", false));
        reasonBeanList.add(new ChangeDriverReasonBean.ReasonTextBean("机械限行", false));
        bean.reason = reasonBeanList;
        list.add(bean);

        ChangeDriverReasonBean bean1 = new ChangeDriverReasonBean();
        bean1.title = "费用原因";
        List<ChangeDriverReasonBean.ReasonTextBean> reasonBeanList1 = new ArrayList<>();
        reasonBeanList1.add(new ChangeDriverReasonBean.ReasonTextBean("费用没有谈拢（高速费、停车费、等候费等）", false));
        reasonBeanList1.add(new ChangeDriverReasonBean.ReasonTextBean("未按平台规定收费", false));
        bean1.reason = reasonBeanList1;
        list.add(bean1);

        ChangeDriverReasonBean bean2 = new ChangeDriverReasonBean();
        bean2.title = "机佬原因";
        List<ChangeDriverReasonBean.ReasonTextBean> reasonBeanList2 = new ArrayList<>();
        reasonBeanList2.add(new ChangeDriverReasonBean.ReasonTextBean("联系不上机佬", false));
        reasonBeanList2.add(new ChangeDriverReasonBean.ReasonTextBean("机佬推诿不来", false));
        reasonBeanList2.add(new ChangeDriverReasonBean.ReasonTextBean("机佬迟到", false));
        reasonBeanList2.add(new ChangeDriverReasonBean.ReasonTextBean("机佬要求线下交易", false));
        reasonBeanList2.add(new ChangeDriverReasonBean.ReasonTextBean("不是单子显示机械或机佬", false));
        bean2.reason = reasonBeanList2;
        list.add(bean2);

        ChangeDriverReasonBean bean3 = new ChangeDriverReasonBean();
        bean3.title = "平台原因";
        List<ChangeDriverReasonBean.ReasonTextBean> reasonBeanList3 = new ArrayList<>();
        reasonBeanList3.add(new ChangeDriverReasonBean.ReasonTextBean("机佬距离太远，不愿意等待", false));
        bean3.reason = reasonBeanList3;
        list.add(bean3);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, list, R.layout.item_change_driver);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        adapter.setItemDecorator((BaseDataBindingDecorator<ChangeDriverReasonBean, ItemChangeDriverBinding>) (binding, position, viewType, data) -> {

            // 不可嵌套滑动
            binding.itemRecyclerView.setNestedScrollingEnabled(false);
            // 取消head 的 recycleView的焦点
            binding.itemRecyclerView.setFocusableInTouchMode(false);
            //设置布局管理器
            binding.itemRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            SingleTypeBindingAdapter itemAdapter = new SingleTypeBindingAdapter(this, data.reason, R.layout.item_change_driver_item);
            binding.itemRecyclerView.setAdapter(itemAdapter);
            itemAdapter.setItemPresenter(this);

        });

    }

    @Override
    public void onReasonItemClick(int position, ChangeDriverReasonBean itemData) {
        itemData.isClick = !itemData.isClick;
        adapter.refresh();

    }

    //确定
    @Override
    public void onDetermine() {
        if (TextUtils.isEmpty(reasonText)){
            ToastUtils.showShort("请选择更换原因");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("orderId", orderId);
        map.put("reason", URLEncoder.encode(reasonText));
        map.put("userType",  Constant.USER_TYPE);
        map.put("udid",  PhoneUtils.getUDID());

        mViewModel.cancelOrder(map);
    }

    @Override
    public void returnCancelOrder() {
        ToastUtils.showShort("更换司机成功");
      /*  //等待接单
        IntentManager.toWaitingOrderActivity(this, orderId, orderParameters, marketPriceBean, orderParameters.amount, false, "");
        AppActivityManager.getAppActivityManager().returnToActivity(HomeActivity.class);*/
        finish();
    }

    @Override
    public void onItemClick(int position, ChangeDriverReasonBean.ReasonTextBean itemData) {
        if (itemData.isClick) return;
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < list.get(i).reason.size(); j++) {
                list.get(i).reason.get(j).isClick=false;
            }
        }
        itemData.isClick = true;
        reasonText =itemData.text;
        adapter.refresh();
    }
}
